package com.xpgk.common.config;


import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.CaseInsensitiveContainerFactory;
import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.plugin.ehcache.EhCachePlugin;
import com.jfinal.template.Engine;
import com.jfinal.template.source.ClassPathSourceFactory;
import com.xpgk.common.handlers.GlobalHandler;
import com.xpgk.common.handlers.WebSocketHandler;
import com.xpgk.common.interceptors.AuthInterceptor;
import com.xpgk.common.kit.PasswordKit;
import com.xpgk.common.kit.PubKit;
import com.xpgk.kcys.route.Kcys_Route;
import com.xpgk.model._MappingKit;
import com.xpgk.xpsoft.constants.XPSoftConstant;
import com.xpgk.xpsoft.route.XPSoft_Route;


public class Config extends JFinalConfig{

	private static final Log log = Log.getLog(Config.class);
	
	public static Prop prop = PropKit.use("config.properties"); 
	
	@Override
	public void configConstant(Constants me) {
		// TODO Auto-generated method stub
//		log.debug("常量设置");
		PropKit.use("config.properties");
		//热加载
		me.setDevMode(true);
		if (me.getErrorView(404) == null)
			me.setError404View("/_common/404.html");
		if (me.getErrorView(500) == null)
			me.setError500View("/_common/500.html");

		XPSoftConstant.systemID = PropKit.getInt("systemID");
		if (XPSoftConstant.systemID == null)
			PasswordKit.setProp("systemID", "1");
	}

	@Override
	public void configEngine(Engine me) {
		// TODO Auto-generated method stub
		me.addSharedFunction("/_common/fun/_layout.html");
		me.addSharedFunction("/_common/fun/_option.html");
	}

	@Override
	public void configHandler(Handlers me) {
		// TODO Auto-generated method stub
		me.add(new GlobalHandler());//全局处理器
//		me.add(new WebSocketHandler("^/websocket"));
	}

	@Override
	public void configInterceptor(Interceptors me) {
		// TODO Auto-generated method stub
		
		log.info("configInterceptor 权限认证拦截器");
		me.add(new AuthInterceptor());
	}

	/**
	 * 抽取成独立的方法，便于 _Generator 中重用该方法，减少代码冗余
	 */
	public static DruidPlugin getDruidPlugin() {
		return new DruidPlugin(prop.get("jdbcUrl"),prop.get("user"),prop.get("password").trim());
	}
	
	@Override
	public void configPlugin(Plugins me) {
		me.add(new EhCachePlugin());
		// TODO Auto-generated method stub
		ActiveRecordPlugin arp = null;
		String jdbcUrl = PropKit.get("jdbcUrl");
		if(!PubKit.isBlank(jdbcUrl)){
			DruidPlugin druidPlugin = getDruidPlugin();
			me.add(druidPlugin);
			arp = new ActiveRecordPlugin(druidPlugin);
			
			//配置数据库方言
			arp = new ActiveRecordPlugin("mysql", druidPlugin);
			arp.setDialect(new MysqlDialect());
			// 配置属性名(字段名)大小写不敏感容器工厂
			arp.setContainerFactory(new CaseInsensitiveContainerFactory());
		}
		
		if(!PubKit.isBlank(arp)){
//			_MappingKit.mapping(arp);
//			me.add(arp);
			 _MappingKit.mapping(arp); 
			// String classPath=PathKit.getRootClassPath();
			arp.setBaseSqlTemplatePath(null);
			arp.getEngine().setSourceFactory(new ClassPathSourceFactory());
			arp.addSqlTemplate("/all.sql");
			arp.setShowSql(true);
			me.add(arp);
		}
		
	}

	@Override
	public void configRoute(Routes me) {
		// TODO Auto-generated method stub
		me.add(new XPSoft_Route());//系统路由(登录、用户、部门、岗位、权限等)
		me.add(new Kcys_Route());//业务路由(项目具体业务等)
	}

	/**
	 * 系统启动完成后执行
	 */
	public void afterJFinalStart() {
//		log.info("afterJFinalStart 启动操作日志入库线程");
		// ThreadSysLog.startSaveDBThread();
		System.out.println("afterJFinalStart");
	}

	/**
	 * 系统关闭前调用
	 */
	public void beforeJFinalStop() {
//		log.info("beforeJFinalStop 释放日志入库线程");
		// ThreadSysLog.setThreadRun(false);
		System.out.println("beforeJFinalStop");
	}
}
